import numpy as np
import pandas as pd
df=pd.read_csv("C:/Users/sridi/final_mediation.csv")
df.head()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['WellBeing_3']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1',
'QOLmental_2', 'QOLphysical_2', 'Flourishing_2', 'Loneliness_2',
'HealthMotivation_2', 'Relatedness_2', 'Anxiety_2', 'Depression_2',
'TechComfort_2', 'TechPhyLim_2', 'WellBeing_2']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['WellBeing_3'])
data_target.shape
data['WellBeing_3'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1',
'QOLmental_2', 'QOLphysical_2', 'Flourishing_2', 'Loneliness_2',
'HealthMotivation_2', 'Relatedness_2', 'Anxiety_2', 'Depression_2',
'TechComfort_2', 'TechPhyLim_2', 'WellBeing_2']]
y = data['WellBeing_3']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['QOLmental_2']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLmental_2'])
data_target.shape
data['QOLmental_2'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['QOLmental_2']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['QOLphysical_2']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLphysical_2'])
data_target.shape
data['QOLphysical_2'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['QOLphysical_2']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Loneliness_2']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Loneliness_2'])
data_target.shape
data['Loneliness_2'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['Loneliness_2']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Relatedness_2']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Relatedness_2'])
data_target.shape
data['Relatedness_2'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['Relatedness_2']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Flourishing_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Flourishing_1'])
data_target.shape
data['Flourishing_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['Flourishing_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['QOLmental_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLmental_1'])
data_target.shape
data['QOLmental_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['QOLmental_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['QOLphysical_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['QOLphysical_1'])
data_target.shape
data['QOLphysical_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['QOLphysical_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['TechComfort_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['TechComfort_1'])
data_target.shape
data['TechComfort_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7', 'QOLmental_1', 'QOLphysical_1', 'Flourishing_1',
'Loneliness_1', 'HealthMotivation_1', 'Relatedness_1', 'Anxiety_1',
'Depression_1', 'TechComfort_1', 'TechPhyLim_1', 'WellBeing_1']]
y = data['TechComfort_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Loneliness_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Loneliness_1'])
data_target.shape
data['Loneliness_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['Loneliness_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['HealthMotivation_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['HealthMotivation_1'])
data_target.shape
data['HealthMotivation_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['HealthMotivation_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(70, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Depression_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Depression_1'])
data_target.shape
data['Depression_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['Depression_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()
from sklearn.linear_model import Lasso
# initiate lasso regression model:
model = Lasso()
# define predictor & response variables:
# define response variable:
y = df['Relatedness_1']
# define predictor variables:x = df[['hours', 'exams']]
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
# fit regression model:
model.fit(X, y)
data = pd.DataFrame(df)
data.columns = df.columns
data_target = np.asarray(df['Relatedness_1'])
data_target.shape
data['Relatedness_1'] = pd.Series(data_target)
data
X = df[['Arm', 'QOLmental_0', 'QOLphysical_0', 'Flourishing_0', 'Loneliness_0',
'HealthMotivation_0', 'Relatedness_0', 'Anxiety_0', 'Depression_0',
'TechComfort_0', 'TechPhyLim_0', 'WellBeing_0', 'partner',
'live_alone', 'education_4', 'education_5', 'education_6',
'education_7']]
y = data['Relatedness_1']
print(X)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2,
random_state = 1)
print("Train data shape of X = % s and y = % s : "%(X_train.shape, y_train.shape))
print("Test data shape of X = % s and y = % s : "%(X_test.shape, y_test.shape))
import statsmodels.api as sm
from sklearn.linear_model import Lasso
# Train the model:
lasso = Lasso(alpha = 1)
lasso.fit(X_train, y_train)
y_pred = lasso.predict(X_test)
# Calculate Mean Squared Error:
#mean_squared_error = np.mean((y_pred1 - y_test)**2)
#print("Mean squared error on test set:", mean_squared_error)
# Model Predictions:
#from sklearn.metrics import r2_score
#print("R-squared score on test set:", r2_score(y_test, y_pred), "\n")
# Fit regression model:
model = sm.OLS(y, X).fit()
# Display adjusted R-squared
print("Adjusted R-squared:", model.rsquared_adj, "\n")
lasso_coeff = pd.DataFrame()
lasso_coeff["Columns"] = X_train.columns
lasso_coeff['Coefficient Estimate'] = pd.Series(lasso.coef_)
print(lasso_coeff)
# Plot a bar chart of above coefficients using matplotlib:
import matplotlib.pyplot as plt
# plotting the coefficient score:
fig, ax = plt.subplots(figsize =(100, 50))
color =['tab:gray', 'tab:blue', 'tab:orange',
'tab:green', 'tab:red', 'tab:purple', 'tab:brown',
'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan',
'tab:orange', 'tab:green', 'tab:blue', 'tab:olive']
plt.bar(lasso_coeff["Columns"],
lasso_coeff['Coefficient Estimate'],
color = color)
# plotting x- & y-axes:
ax.spines['left'].set_position(('axes', 0))
ax.spines['bottom'].set_position(('axes', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.style.use('ggplot')
# Set general font size:
plt.rcParams['font.size'] = '100'
# Rotate x-axis tick labels:
plt.xticks(rotation = 90)
plt.show()